package projectwork.shchuangge.cn.com.newproject.cachemanger;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import projectwork.shchuangge.cn.com.newproject.projectwork.Chuangge;

/**
 * Created by 赵航 on 2017/6/7.
 */

public class CacheFileManger {
    private static final CacheFileManger ourInstance = new CacheFileManger();
    private String mPath;
    private File mFile;

    public static CacheFileManger getInstance() {
        return ourInstance;
    }

    private CacheFileManger() {
        mPath = Environment.getExternalStorageDirectory().getPath() + File.separator + Chuangge.context.getPackageName() + File.separator + "chach";
        mFile = new File(mPath);
        //这里是没办法生成文件的，多级目录必须手动创建
        if (!mFile.exists()){

            //这里手动创建多级目录
            mFile.mkdirs();

        }
    }

    /**
     * 跟距url返回对应的数据
     * @param url
     */
    public String getCacheData(String url) {
        StringBuffer stringBuffer = new StringBuffer();

        File file=new File(mFile,getFileName(url));
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            int len = -1;
            byte[] buffer = new byte[1024];
            while ((len=fileInputStream.read(buffer))!=-1){
                stringBuffer.append(new String(buffer,0,len));
            }
            if (fileInputStream!=null){
                fileInputStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();


            return null;

        }

        return stringBuffer.toString();
    }


    //保存数据不需要返回结果
    public void saveCachData(String url, String context) {

        /**
         * 保存到SDK卡/包名/Cach/xxx.txt
         * File.separator是跟距不同平台可以自动翻译为“/"的
         */
        try {
//            mPath = Environment.getExternalStorageDirectory().getPath() + File.separator + GoogleApp.context.getPackageName() + File.separator + "chach";
//            mFile = new File(mPath);
//            //这里是没办法生成文件的，多级目录必须手动创建
//            if (!mFile.exists()){
//
//                //这里手动创建多级目录
//                mFile.mkdirs();
//
//            }
            File fileName = new File(mFile, getFileName(url));

            FileOutputStream outputStream = new FileOutputStream(fileName);
            //写入数据

            outputStream.write(context.getBytes());

            //关流
            if(outputStream!=null){
                outputStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private String getFileName(String url) {
        StringBuffer stringBuffer = new StringBuffer();

        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            //加密文件的字节流
            messageDigest.update(url.getBytes());
            byte[] bytes = messageDigest.digest();
            //将bytes数组转化为16字节的字符串
            for (int i = 0; i < bytes.length; i++) {
                String s = Integer.toHexString(bytes[i] & 0XFF);
                stringBuffer.append(s);
            }


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        return stringBuffer.toString();
    }
}
